package descentFly.app.sight
{
	import UIAway.uif_view;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_bitmap;
	
	import UIUtils.uif_utilBitmap;
	
	import descentFly.app.df_component;
	import descentFly.away3D4.chips.df_chip3DS;
	import descentFly.away3D4.chips.df_chipAway;
	import descentFly.away3D4.chips.df_chipModel;
	import descentFly.away3D4.chips.df_monster;
	import descentFly.away3D4.controller.df_doorAway;
	import descentFly.away3D4.df_awaySpace;
	import descentFly.away3D4.doors.df_door;
	import descentFly.away3D4.doors.df_door3DS;
	import descentFly.chips.df_chip;
	import descentFly.chips.df_weapont;
	import descentFly.data.df_sights;
	import descentFly.df_space;
	import descentFly.server.df_servers;
	import descentFly.view.df_view;
	import descentFly.weaponts.df_awayWeapont;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.system.System;
	
	public class df_sight extends df_component {
		private var _myChip		: df_chip;
		private var _sight		: df_bitmapSight;
		private var _targetRing	: df_targetRing;
		public function df_sight(parent: df_component) {
			super(parent);
			_targetRing = new df_targetRing(this, view, 60);
		}
		
		protected function get view(): uif_view {
			return (Space as df_awaySpace).view;
		}
		
		override protected function listenEvents(): void {
			super.listenEvents();
			addEvent(df_space.SETMYCHIP, doSetMyChip, Space);
			addEvent(df_space.RENDER, doRender, Space);
		}
		
		private function doSetMyChip(e: ObjectEvent): void {
			disposeSight();
			if (_myChip) removeMyChipEvents();
			_myChip = e.object as df_chip;
			if (_myChip) listenMyChipEvents();
		}
		
		protected function removeMyChipEvents(): void {
			_myChip.removeEventListener(df_chip.CHANGEWEAPONT, doChangeWeapoint);
		}
		
		protected function listenMyChipEvents(): void {
			_myChip.addEventListener(df_chip.CHANGEWEAPONT, doChangeWeapoint);
		}
		
		private function doRender(e: Event): void {
			if (_myChip) calcSightSpot();
		}
		
		protected function calcSightSpot(): void {
			var chip: df_chipAway = _myChip as df_chipAway;
			var weapont: df_awayWeapont = chip.currentWeapont as df_awayWeapont;
			
			var p: Vector3D = weapont.attachmentPoint;
			var start: Vector3D = chip.body.skin.transform.transformVector(p);
			
			p.z += weapont.distance;
			var	distance: Vector3D = chip.body.skin.transform.transformVector(p);
			
			var ray: Vector3D = distance.subtract(start);
			
			var hitChip: df_chipAway = null;
			var hitChipMin: Number = Number.MAX_VALUE;
			
			Space.forEach(function(chip: df_chipModel, i: int): void {
				if ((chip != _myChip) && (chip.model.visible) && (chip.attackable)) {
					var hitDistance: Number = chip.checkHit(start, ray);
					if ((hitDistance > -1) && (hitChipMin > hitDistance)) {
						hitChip = chip;
						hitChipMin = hitDistance;
					}
				}
			}, df_chipModel);
			
			if (hitChip && (hitChipMin < weapont.distance)) {
				p = weapont.attachmentPoint;
				p.z += hitChipMin;
				distance = chip.body.skin.transform.transformVector(p);
//				distance = hitChip.body.currentState.position;
				_targetRing.hitObject(hitChip, hitChipMin);
			} else {
				_targetRing.hitObject(null);
				
				var rtResult: Object = (Space as df_awaySpace).envRayTest(start, distance, true);
				if (rtResult) {
					p = chip.body.currentState.orientation.transformVector(weapont.attachmentPoint);
					distance = rtResult.hitPoint.add(p);
				}
			}
			
			_sight.position = view.position.add(view.view.project(distance));
		}
		
		private function disposeSight(): void {
			if (_sight) {
				_sight.dispose();
				_sight = null;
			}
		}
		
		private function doChangeWeapoint(e: Event): void {
			disposeSight();
			var sightObject: Object = df_sights.list[0];
			_sight = new (sightObject['class'])(this, sightObject);
		}
	}
}